package com.auto.learning.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.adapter.ImageBucketAdapter;
import com.auto.learning.bean.ImageBucket;
import com.auto.learning.net.HttpRequestHelper;
import com.auto.learning.tools.ImageFetcher;
import com.auto.learning.tools.IntentConstants;

/**
 * 选择相册
 */
public class ImageBucketChooseActivity extends Activity{
	private ImageFetcher mHelper;
	private List<ImageBucket> mDataList = new ArrayList<ImageBucket>();
	private ListView mListView;
	private ImageBucketAdapter mAdapter;
	private int availableSize;
	private TextView tv_action_right;

	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_image_bucket_choose);
		mHelper = ImageFetcher.getInstance(getApplicationContext());
		initData();
		initView();
	}

	private void initData(){
		mDataList = mHelper.getImagesBucketList(true);
		availableSize = getIntent().getIntExtra(IntentConstants.EXTRA_CAN_ADD_IMAGE_SIZE, HttpRequestHelper.MAX_IMAGE_SIZE);
	}

	private void initView(){
		tv_action_right = (TextView) findViewById(R.id.tv_action_right);
		tv_action_right.setVisibility(View.GONE);
		mListView = (ListView) findViewById(R.id.listview);
		mAdapter = new ImageBucketAdapter(this, mDataList);
		mListView.setAdapter(mAdapter);
		TextView titleTv  = (TextView) findViewById(R.id.title);
		titleTv.setText("相册");
		mListView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				selectOne(position);
				Intent intent = new Intent(ImageBucketChooseActivity.this,ImageChooseActivity.class);
				intent.putExtra(IntentConstants.EXTRA_IMAGE_LIST,(Serializable) mDataList.get(position).imageList);
				intent.putExtra(IntentConstants.EXTRA_BUCKET_NAME,mDataList.get(position).bucketName);
				intent.putExtra(IntentConstants.EXTRA_CAN_ADD_IMAGE_SIZE,availableSize);
				startActivity(intent);
			}
		});

		TextView cancelTv = (TextView) findViewById(R.id.tv_action_left);
		cancelTv.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){
				finish();
			}
		});
	}

	private void selectOne(int position) {
		int size = mDataList.size();
		for (int i = 0; i != size; i++) {
			if (i == position)
				mDataList.get(i).selected = true;
			else {
				mDataList.get(i).selected = false;
			}
		}
		mAdapter.notifyDataSetChanged();
	}

}
